package com.imay.live.helper;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View.OnClickListener;

import com.imay.live.view.dialog.ConfirmDialogFragment;
import com.imay.live.view.dialog.LoadingDialogFragment;
import com.imay.live.view.dialog.TipsDialogFragment;


public class DialogFragmentHelper {
    private static final String DIALOG_TAG_CONFIRM = "dialog_confirm";//确定取消提示框
    private static final String DIALOG_TAG_TIPS = "dialog_tips";
    private static final String DIALOG_TAG_LOADING = "dialog_loading";

    /**
     * 默认为取消，确认按钮的选择提示
     */
    public static void showConfirmDialog(FragmentManager fragmentManager, String message, OnClickListener positiveListener) {
        showConfirmDialog(fragmentManager, null, message, null, null, true, positiveListener, null);
    }

    public static void showConfirmDialog(FragmentManager fragmentManager, String message, OnClickListener positiveListener, OnClickListener negativeListener) {
        showConfirmDialog(fragmentManager, null, message, null, null, true, positiveListener, negativeListener);
    }

    public static void showConfirmDialog(FragmentManager fragmentManager, String title, String message, OnClickListener positiveListener) {
        showConfirmDialog(fragmentManager, title, message, null, null, true, positiveListener, null);
    }

    public static void showConfirmDialog(FragmentManager fragmentManager, String title, String message, String positive, OnClickListener positiveListener) {
        showConfirmDialog(fragmentManager, title, message, positive, null, true, positiveListener, null);
    }

    public static void showConfirmDialog(FragmentManager fragmentManager, String title, String message, String positive, String negative, boolean cancelable, OnClickListener positiveListener, OnClickListener negativeListener) {
        removeFragment(fragmentManager, DIALOG_TAG_CONFIRM);
        ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(title, message, positive, negative, cancelable);
        dialogFragment.setPositiveButton(positiveListener);
        dialogFragment.setNegativeButton(negativeListener);
        showDialogFragment(fragmentManager, dialogFragment, DIALOG_TAG_CONFIRM);
    }

    //只有确定的对话框
    public static void showTipsDialog(FragmentManager fragmentManager, String message) {
       showTipsDialog(fragmentManager, message, null);
    }

    //只有确定的对话框
    public static void showTipsDialog(FragmentManager fragmentManager, String message, OnClickListener mPositiveListener) {
        showTipsDialog(fragmentManager, message, true, mPositiveListener);
    }

    //只有确定的对话框
    public static void showTipsDialog(FragmentManager fragmentManager, String message, boolean cancelable, OnClickListener mPositiveListener) {
        showTipsDialog(fragmentManager, DIALOG_TAG_TIPS, message, cancelable, mPositiveListener);
    }

    //只有确定的对话框
    public static void showTipsDialog(FragmentManager fragmentManager, String tag, String message, boolean cancelable, OnClickListener mPositiveListener) {
        removeFragment(fragmentManager, tag);
        TipsDialogFragment dialogFragment = TipsDialogFragment.newInstance(null, message, null, cancelable);
        dialogFragment.setPositiveButton(mPositiveListener);
        showDialogFragment(fragmentManager, dialogFragment, tag);
    }

    //显示加载对话框
    public static void showLoadingDialog(FragmentManager fragmentManager, String message) {
        showLoadingDialog(fragmentManager, message, true, DIALOG_TAG_LOADING);
    }

    //显示加载对话框
    public static void showLoadingDialog(FragmentManager fragmentManager, String message, boolean cancelable) {
        showLoadingDialog(fragmentManager, message, cancelable, DIALOG_TAG_LOADING);
    }

    //显示加载对话框
    public static void showLoadingDialog(FragmentManager fragmentManager, String message, boolean cancelable, String tag) {
        closeLoadingDialog(fragmentManager);
        LoadingDialogFragment dialogFragment = LoadingDialogFragment.newInstance(message, cancelable);
        showDialogFragment(fragmentManager, dialogFragment, tag);
    }

    //关闭加载对话框
    public static void closeLoadingDialog(FragmentManager fragmentManager) {
        removeFragment(fragmentManager, DIALOG_TAG_LOADING);
    }

    //加载对话框
    public static void closeDialog(FragmentManager fragmentManager, String tag) {
        removeFragment(fragmentManager, tag);
    }

    //显示自定义对话框
    public static void showDialogFragment(FragmentManager fragmentManager, DialogFragment dialogFragment) {
        showDialogFragment(fragmentManager, dialogFragment, dialogFragment.getClass().getName());
    }

    //显示自定义对话框
    public static void showDialogFragment(FragmentManager fragmentManager, DialogFragment dialogFragment, String tag) {
        if (!fragmentManager.isDestroyed()) {
            try {
                removeFragment(fragmentManager, tag);
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.add(dialogFragment, tag);
                transaction.commitAllowingStateLoss();
//        dialogFragment.show(fragmentManager, tag);    `//直接用show方法可能会抛异常Can not perform this action after onSaveInstanceState
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }

    public static Fragment getFragment(FragmentManager fragmentManager, String tag) {
        return fragmentManager.findFragmentByTag(tag);
    }

    //移除对话框
    private static void removeFragment(FragmentManager fragmentManager, String tag){
        FragmentTransaction ft = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(tag);
        if (fragment != null) {
            ft.remove(fragment);
            ft.commitNowAllowingStateLoss();
        }
    }
}
